package com.android.yinghebike.ui.fragment;

import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;

import com.android.baselibrary.dialog.CommList_DialogFragment;
import com.android.baselibrary.dialog.Comm_DialogFragment;
import com.android.baselibrary.interface_.CommCallBack;
import com.android.baselibrary.ui.BaseFragment;
import com.android.baselibrary.view.Comm_HeadView;
import com.android.baselibrary.view.Comm_TitleView;
import com.android.yinghebike.R;
import com.android.yinghebike.bean.UserInfo;
import com.android.yinghebike.manager.BleManager;
import com.android.yinghebike.manager.LanguageManager;
import com.android.yinghebike.manager.UnitManager;
import com.android.yinghebike.manager.UserManager;
import com.android.yinghebike.ui.activity.AboutUsActivity;
import com.android.yinghebike.ui.activity.AccountActivity;
import com.android.yinghebike.ui.activity.MainActivity;
import com.android.yinghebike.ui.activity.PermissionSettingActivity;
import com.android.yinghebike.ui.activity.UpdateActivity;
import com.android.yinghebike.utils.LocaleUtils;
import com.gyf.immersionbar.ImmersionBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class HomeMineFragment extends BaseFragment {

    @BindView(R.id.comm_title)
    Comm_HeadView comm_title;
    @BindView(R.id.tv_account)
    TextView tv_account;
    @BindView(R.id.ctv_unit)
    Comm_TitleView ctv_unit;
    @BindView(R.id.ctv_language)
    Comm_TitleView ctv_language;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView() {
        comm_title.setTitle(getResources().getString(R.string.mine_title));
        comm_title.setLeftImgVisible(View.GONE);

        ImmersionBar.with(this).statusBarDarkFont(false).titleBar(comm_title).navigationBarDarkIcon(false).init();

    }

    @Override
    protected void initData() {
        UserInfo userInfo = UserManager.getUserInfo(mContext);
        tv_account.setText(userInfo.account);

        int unitType = UnitManager.getUnitType();
        ctv_unit.setContent(unitType == 0 ? getString(R.string.mine_distance_unit_metric) : getString(R.string.mine_distance_unit_imperial));

        int languageType = LanguageManager.getLanguageType();
        ctv_language.setContent(languageType == 0 ? getString(R.string.mine_language_en) : getString(R.string.mine_language_zh));


    }

    @OnClick({R.id.ctv_account, R.id.ctv_permission, R.id.ctv_about, R.id.csb_logout, R.id.ctv_update, R.id.ctv_unit, R.id.ctv_language})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ctv_account:   //点击账户
                StartActivity(AccountActivity.class);
                break;
            case R.id.ctv_permission:   //权限
                StartActivity(PermissionSettingActivity.class);
                break;
            case R.id.ctv_about:   //关于
                StartActivity(AboutUsActivity.class);
                break;
            case R.id.csb_logout:  //退出登录
                Comm_DialogFragment.showCommDialog((FragmentActivity) mContext, "", "确定退出登录?", "确定", "取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UserManager.logout(mContext);
                        ((FragmentActivity) mContext).finish();
                    }
                }, null);
                break;
            case R.id.ctv_update:  //固件升级
                if (BleManager.getInstance().isConnect()) {
                    //发送升级指令
                    byte[] arr = BleManager.getInstance().createControlData((byte) 0x03);
                    BleManager.getInstance().sendData(arr, null);
                }
                BleManager.getInstance().disConnect();
                StartActivity(UpdateActivity.class);
                break;
            case R.id.ctv_unit:
                List<String> list = new ArrayList<>();
                list.add(getString(R.string.mine_distance_unit_metric));
                list.add(getString(R.string.mine_distance_unit_imperial));
                CommList_DialogFragment.showCommDialog(getActivity(), list, true, new CommCallBack() {
                    @Override
                    public void onResult(Object obj) {
                        int index = (int) obj;
                        UnitManager.setUnitType(index);
                        initData();

                        //给蓝牙发单位指令
                        int unitType = UnitManager.getUnitType();
                        byte[] data = BleManager.createSendBleData((byte) 0x81, new byte[]{(byte) ((unitType) & 0xFF)});
                        BleManager.getInstance().sendData(data, null);
                    }
                }, null);
                break;
            case R.id.ctv_language:
                List<String> list1 = new ArrayList<>();
                list1.add(getString(R.string.mine_language_en));
                list1.add(getString(R.string.mine_language_zh));
                CommList_DialogFragment.showCommDialog(getActivity(), list1, true, new CommCallBack() {
                    @Override
                    public void onResult(Object obj) {
                        int index = (int) obj;
                        LanguageManager.setLanguageType(index);
                        initData();

                        LocaleUtils.changeAppLanguage(getActivity(), LanguageManager.getLocale(), MainActivity.class);
                    }
                }, null);
                break;
        }
    }
}
